
var lpnwOpenContainer = null;
var lpnwOpenMenus = null;
var lpnwHideMenuTimer = null;
var lastSelectedListId = null;

function LPNW_SwapImage(obj, imageUrl) {

    if (obj != null && imageUrl != null)
        obj.style.backgroundImage = 'url(\'' + imageUrl + '\')';
}

function LPNW_MenuStartTimeout(hideTimeout) {
    lpnwHideMenuTimer = setTimeout("LPNW_MenuHideMenus()", hideTimeout);
}

function LPNW_NavigateTo(url) {
    window.location = url;
}

function LPNW_MenuHideMenus() {
    LPNW_MenuResetTimeout();
    if (lpnwOpenContainer) {
        var c = document.getElementById(lpnwOpenContainer);
        c.style.visibility = "inherit";
        c.style.display = "none";
		lpnwOpenContainer = null;
	}
	if (lpnwOpenMenus) {
	    for (var i = 0; i < lpnwOpenMenus.length; i++) {
	        var m = document.getElementById(lpnwOpenMenus[i]);
			m.style.visibility = "hidden";			
		}
		lpnwOpenMenus = null;
	}
}

function LPNW_MenuHideSubMenus(menuName) {
	if( lpnwOpenMenus ) {
		var h = false;
		var c = 0;
		for(var i = 0; i < lpnwOpenMenus.length ; i++) {
			if( h ) {
				var m = document.getElementById(lpnwOpenMenus[i]);
				m.style.visibility = "hidden";
			} else if( lpnwOpenMenus[i] == menuName ) {
				h = true;
			} else {
				c++;
			}
		}
		lpnwOpenMenus.length = c+1;
	}
}

function LPNW_MenuOverMenuItem(menuName, subMenuSuffix) {
	LPNW_MenuResetTimeout();
	LPNW_MenuHideSubMenus(menuName);
	if( subMenuSuffix ) {
		var subMenuName = "" + menuName + "_" + subMenuSuffix;
		LPNW_MenuShowSubMenu(subMenuName);
	}
}

function LPNW_MenuShowSubMenu(subMenuName) {
	LPNW_MenuResetTimeout();
	var e = document.getElementById(subMenuName);
	e.style.visibility = "inherit";
	
	if( !lpnwOpenMenus ) {
		lpnwOpenMenus = new Array;
	}
	lpnwOpenMenus[lpnwOpenMenus.length] = "" + subMenuName;
}

function LPNW_MenuResetTimeout() {
    if (lpnwHideMenuTimer) clearTimeout(lpnwHideMenuTimer);
    lpnwHideMenuTimer = null;
}

function LPNW_MenuShowMenu(obj, containName, menuName, xOffset, yOffset, triggerName) {
	LPNW_MenuHideMenus();
	LPNW_MenuResetTimeout();
	LPNW_MenuShowMenuContainer(obj, containName, xOffset, yOffset, triggerName);
	LPNW_MenuShowSubMenu(menuName);
}

function LPNW_MenuShowMenuContainer(obj, containName, x, y, triggerName) {	
	var c = document.getElementById(containName);
	var s = c.style;

	s.visibility = "inherit";
	s.display = "block";
	s.top = LPNW_GetAbsTop(obj) + obj.offsetHeight + y;
	s.left = LPNW_GetAbsLeft(obj) + x;
	lpnwOpenContainer = "" + containName;
}

function LPNW_GetAbsTop(obj) {
    var top = 0;
    var element = obj;
    while (element != null) {
        top += element.offsetTop;
        element = element.offsetParent;
    }
    return top;
}

function LPNW_GetAbsLeft(obj) {
    var left = 0;
    var element = obj;
    while (element != null) {
        left += element.offsetLeft;
        element = element.offsetParent;
    }
    return left;
}

function LPNW_FindObj(n, d) { 
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=LPNW_FindObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function LPNW_InitConfiguration(viewStateId, clientId)
{
    var viewState = document.getElementById(viewStateId);
    
    if(viewState.value.length < 1)
        return;
    
    var xmlDoc = LPNW_GetXmlDocument(viewState.value);
    
    var links=xmlDoc.documentElement.childNodes;
    
    for (i=0;i<links.length;i++)
    { 
        if (links[i].nodeType==1)
        {
            var link = new LibNavLink();
            link.fromXmlNode(links[i]);
            LPNW_AddToListBox(viewStateId, clientId, link);
        }
    }
}

function LPNW_AddToListBox(viewStateId, clientId, link) {
    var listBox = document.getElementById(clientId + "_lnwpListBox");

    var opt = document.createElement("option");  
    opt.text = link.getDisplayName();
    if(link.id != null)
        opt.value = link.id;

    opt.setAttribute("LinkObject", link.toXml());
    // Add an Option object to Drop Down/List Box
    listBox.options.add(opt);

    LPNW_SaveToViewState(viewStateId, clientId);
}

function LPNW_SaveToViewState(viewStateId, clientId) {
    var listBox = document.getElementById(clientId + "_lnwpListBox");
    var viewState = document.getElementById(viewStateId);

    var strLinks = "<Links>";
    
    var i = 0;
    for (i = 0; i < listBox.options.length; i++) {
        var option = listBox.options[i];
        strLinks += option.getAttribute("LinkObject");
    }
    strLinks += "</Links>";
    viewState.value = strLinks;
}

function LPNW_RemoveSelectedItemInListBox(viewStateId, clientId) {
    var listBox = document.getElementById(clientId + "_lnwpListBox");

    for (i = 0; i < listBox.options.length; i++) {
        var opt = listBox.options[i];
        if (opt.selected) {

            if (!confirm("Do you really want to delete this link?"))
                return;

            //Remove selected item
            listBox.options[i] = null;
            
            break;
        }
    }

    listBox.focus();
    
    LPNW_SaveToViewState(viewStateId, clientId);
}

function LPNW_MoveSelectedItemDown(viewStateId, clientId) {
    var listBox = document.getElementById(clientId + "_lnwpListBox");

    LPNW_MoveOptionsDown(listBox);

    listBox.focus();

    LPNW_SaveToViewState(viewStateId, clientId);
}

function LPNW_MoveSelectedItemUp(viewStateId, clientId) {

    var listBox = document.getElementById(clientId + "_lnwpListBox");

    LPNW_MoveOptionsUp(listBox);
    
    listBox.focus();
    
    LPNW_SaveToViewState(viewStateId, clientId);
}

function LPNW_MoveOptionsUp(listBox) 
{
    for (var i = 1; i < listBox.options.length; i++) 
    {
        var opt = listBox.options[i];
        if (opt.selected) {
            var lnkText = opt.text;
            var lnkValue = opt.value;
            var lnkConfig = opt.getAttribute("LinkObject");

            var prevOpt = listBox.options[i - 1];
            listBox.options[i] = null;

            var newOpt = document.createElement("option");
            newOpt = listBox.insertBefore(newOpt, prevOpt);
            newOpt.text = lnkText;
            newOpt.value = lnkValue;
            newOpt.setAttribute("LinkObject", lnkConfig);
            newOpt.selected = true;
            
            break;
        }    
    }
}

function LPNW_MoveOptionsDown(listBox) 
{
    for (var i = listBox.options.length - 2; i >= 0; i--) 
    {
        var opt = listBox.options[i];

        if (opt.selected) {
            var lnkText = opt.text;
            var lnkValue = opt.value;
            var lnkConfig = opt.getAttribute("LinkObject");

            var nextOpt = listBox.options[i + 1];
            listBox.options[i] = null;
            
            var newOpt = document.createElement("option");

            nextOpt = listBox.replaceChild(newOpt, nextOpt);
            listBox.insertBefore(nextOpt, newOpt);
            
            newOpt.text = lnkText;
            newOpt.value = lnkValue;
            newOpt.setAttribute("LinkObject", lnkConfig);
            newOpt.selected = true;
            break;
        }    
    }
}

function LPNW_EditSelectedItem(viewStateId, clientId) {
    var listBox = document.getElementById(clientId + "_lnwpListBox");
    var selectedOption = null;

    if (listBox.selectedIndex > -1) {
        selectedOption = listBox.options[listBox.selectedIndex];
    }

    if (selectedOption == null)
        return;

    var divLinkList = document.getElementById(clientId + "_LinkList");
    var divEditForm = document.getElementById(clientId + "_LinkEditForm");
    var btnOK = document.getElementById(clientId + "_btnEditOk");
    var tboxTitle = document.getElementById(clientId + "_Title_EDITOR");
    var tboxListUrl = document.getElementById(clientId + "_ListUrl_EDITOR");
    var tboxDisplayField = document.getElementById(clientId + "_DisplayField_EDITOR");
    var tboxQuery = document.getElementById(clientId + "_Query_EDITOR");
    var tboxId = document.getElementById(clientId + "_Id");

    var strXml = selectedOption.getAttribute("LinkObject");
    var link = new LibNavLink();
    link.fromXml(strXml);
    
    tboxId.value = link.id;
    tboxTitle.value = link.title;
    tboxDisplayField.value = link.displayField;
    tboxQuery.value = link.query;
    tboxListUrl.value = link.listUrl;

    divLinkList.style.display = "none";
    divEditForm.style.display = "block";

    btnOK.onclick = function() {
        LPNW_EditComplete(viewStateId, clientId);
    }
}

function LPNW_AddNewItemToListBox(viewStateId, clientId) {
    var divLinkList = document.getElementById(clientId + "_LinkList");
    var divEditForm = document.getElementById(clientId + "_LinkEditForm");
    var btnOK = document.getElementById(clientId + "_btnEditOk");
    var tboxTitle = document.getElementById(clientId + "_Title_EDITOR");
    var tboxListUrl = document.getElementById(clientId + "_ListUrl_EDITOR");
    var tboxDisplayField = document.getElementById(clientId + "_DisplayField_EDITOR");
    var tboxQuery = document.getElementById(clientId + "_Query_EDITOR");

    tboxTitle.value = "New Link";
    tboxDisplayField.value = "";
    tboxQuery.value = "";
    tboxListUrl.value = "";

    divLinkList.style.display = "none";
    divEditForm.style.display = "block";

    btnOK.onclick = function() {
        LPNW_AddComplete(viewStateId, clientId);
    }
}

function LPNW_AddComplete(viewStateId, clientId) {
    var tboxTitle = document.getElementById(clientId + "_Title_EDITOR");
    var tboxListUrl = document.getElementById(clientId + "_ListUrl_EDITOR");
    var tboxDisplayField = document.getElementById(clientId + "_DisplayField_EDITOR");
    var tboxQuery = document.getElementById(clientId + "_Query_EDITOR");

    if (!LPNW_ValidateControls(tboxTitle, tboxListUrl, tboxDisplayField, tboxQuery))
        return;

    var link = new LibNavLink();
    link.title = tboxTitle.value;
    link.displayField = tboxDisplayField.value;
    link.listUrl = tboxListUrl.value;
    link.query = tboxQuery.value;

    LPNW_AddToListBox(viewStateId, clientId, link);
    LPNW_CancelEditOrAdd(clientId);
}

function LPNW_EditComplete(viewStateId, clientId) {
    var tboxTitle = document.getElementById(clientId + "_Title_EDITOR");
    var tboxListUrl = document.getElementById(clientId + "_ListUrl_EDITOR");
    var tboxDisplayField = document.getElementById(clientId + "_DisplayField_EDITOR");
    var tboxQuery = document.getElementById(clientId + "_Query_EDITOR");
    var tboxId = document.getElementById(clientId + "_Id");

    if (!LPNW_ValidateControls(tboxTitle, tboxListUrl, tboxDisplayField, tboxQuery))
        return;

    var option = LPNW_FindLinkOption(clientId, tboxId.value);
    if (option == null) {
        LPNW_CancelEditOrAdd(clientId);
        return;
    }

    var link = new LibNavLink();
    link.id = tboxId.value;
    link.title = tboxTitle.value;
    link.displayField = tboxDisplayField.value;
    link.listUrl = tboxListUrl.value;
    link.query = tboxQuery.value;

    option.text = link.getDisplayName();
    option.setAttribute("LinkObject", link.toXml());
    
    LPNW_SaveToViewState(viewStateId, clientId);
    LPNW_CancelEditOrAdd(clientId);
}

function LPNW_ValidateControls(tboxTitle, tboxListUrl, tboxDisplayField, tboxQuery) {

    if (LPNW_IsStringEmpty(tboxListUrl.value))
    {
        alert("List Name is a required field.");
        return false;
    }
    if (LPNW_IsStringEmpty(tboxTitle.value))
    {
        alert("Title is a required field.");
        return false;
    }
    return true;
}

function LPNW_IsStringEmpty(value) {
    value = value.replace(/^\s+|\s+$/g, "");
    return value == "" ? true : false;
}

function LPNW_LaunchListPicker(viewStateId, clientId, serverUrl) {

    var callback = function(results) {
        LPNW_SetList(clientId, results);
    };

    LaunchPickerTreeDialog('CbqPickerSelectListTitle', 'CbqPickerSelectListText', 'listsOnly', '', serverUrl, lastSelectedListId, '', '', '/_layouts/images/smt_icon.gif', '', callback);
}

function LPNW_SetList(clientId, results) {
    var listTextBox = document.getElementById(clientId + "_ListUrl_EDITOR");

    if (results == null
        || results[1] == null
        || results[2] == null) return;

    if (results[2] == "") {
        alert("You must select a list!.");
        return;
    }

    lastSelectedListId = results[0];
    var listUrl = '';
    if (listUrl.substring(listUrl.length - 1) != '/')
        listUrl = listUrl + '/';
    if (results[1].charAt(0) == '/')
        results[1] = results[1].substring(1);
    listUrl = listUrl + results[1];
    if (listUrl.substring(listUrl.length - 1) != '/')
        listUrl = listUrl + '/';
    if (results[2].charAt(0) == '/')
        results[2] = results[2].substring(1);
    listUrl = listUrl + results[2];
    listTextBox.value = listUrl;
}

function LPNW_LaunchQueryBuilder(viewStateId, clientId, serverUrl) {
    
    var queryTextBox = document.getElementById(clientId + "_Query_EDITOR");
    var listTextBox = document.getElementById(clientId + "_ListUrl_EDITOR");
    
    if(listTextBox.value.replace(/^\s+|\s+$/g,"") == "")
    {
        alert("A list must be selected before building a query.");
        return;
    }
        
    var callback = function(results) {
        LPNW_SetQuery(clientId, results);
    };

    var query = queryTextBox.value;
    query = Mullivan.Utilities.UrlEncode(query);
    
    QB_LaunchQueryBuilderDialog(serverUrl, listTextBox.value, null, query, false, true, false, callback);
}

function LPNW_SetQuery(clientId, results) {
    var queryTextBox = document.getElementById(clientId + "_Query_EDITOR");

    if (results == null || results.length < 2)
        return;

    //Index 1 contains the query and 0 is for the view if it is being used.
    queryTextBox.value = results[1];
}

function LPNW_FindLinkOption(clientId, linkId) {
    var listBox = document.getElementById(clientId + "_lnwpListBox");
    
    var i = 0;
    for (i = 0; i < listBox.options.length; i++) {
        var option = listBox.options[i];
        if (option.value == linkId) {
            return option;
        }
    } 
    return null;
}

function LPNW_CancelEditOrAdd(clientId) {
    var divLinkList = document.getElementById(clientId + "_LinkList");
    var divEditForm = document.getElementById(clientId + "_LinkEditForm");
    divEditForm.style.display = "none";
    divLinkList.style.display = "block";
}

function LPNW_GetXmlDocument(strXml)
{
    var xmlDoc = null;
    try //Internet Explorer
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async="false";
        xmlDoc.loadXML(strXml);
    }
    catch(e)
    {
        try //Firefox, Mozilla, Opera, etc.
        {
            parser=new DOMParser();
            xmlDoc = parser.parseFromString(strXml, "text/xml");
        }
        catch(e) 
        {   
            alert(e.message);
            return null;
        }
    }
    return xmlDoc;
}

function LibNavLink(id, title, listUrl, displayField, query) {
    this.id = id;
    if (this.id == null || this.id == "")
        this.id = Mullivan.Utilities.NewGuid();
    this.title = title;
    if (this.title == null)
        this.title = "";
    this.listUrl = listUrl;
    if (this.listUrl == null)
        this.listUrl = "";
    this.displayField = displayField;
    if (this.displayField == null)
        this.displayField = "";
    this.query = query;
    if (this.query == null)
        this.query = "";
    this.toXml = LibNavLink_ToXml;
    this.fromXml = LibNavLink_FromXml;
    this.fromXmlNode = LibNavLink_FromXmlNode;
    this.getDisplayName = LibNavLink_GetDisplayName;

    function LibNavLink_FromXml(strXml) {
        var xmlDoc = LPNW_GetXmlDocument(strXml);
        this.fromXmlNode(xmlDoc.documentElement);
    }
    
    function LibNavLink_FromXmlNode(linkNode) {
        var i;
        var x;

        var childNodes = linkNode.childNodes;
        for (i = 0; i < childNodes.length; i++) {
            x = childNodes[i];
            if (x.nodeType == 1
            && x.nodeName == "Query") {
                this.query = x.text; //Already XmlDecoded
                if (this.query == null) {
                    try
                    {
                        //For Firefox
                        this.query = x.textContent;
                    }
                    catch(e)
                    {}
                }
                
                if(this.query == null)
                    this.query = "";
            }
        }

        var attNodes = linkNode.attributes;
        for (i = 0; i < attNodes.length; i++) {
            x = attNodes[i];
            if (x.nodeType == 2) //Attribute
            {
                switch (x.nodeName) {
                    case "title":
                        this.title = x.nodeValue;
                        break;
                    case "listUrl":
                        this.listUrl = x.nodeValue;
                        break;
                    case "displayField":
                        this.displayField = x.nodeValue;
                        break;
                    case "id":
                        this.id = x.nodeValue;
                        break;
                    default:
                        break;
                }
            }
        }
    }

    function LibNavLink_ToXml() {
        var strXml = "";
        strXml += "<LibNavLink id=\"";
        if (this.id != null)
            strXml += this.id;
        strXml += "\" title=\"";
        if (this.title != null)
            strXml += this.title;
        strXml += "\" listUrl=\"";
        if (this.listUrl != null)
            strXml += this.listUrl;
        strXml += "\" displayField=\"";
        if (this.displayField != null)
            strXml += this.displayField;
        strXml += "\"><Query>"
        if (this.query != null)
            strXml += Mullivan.Utilities.XmlEncode(this.query);
        strXml += "</Query></LibNavLink>";
        return strXml;
    }

    function LibNavLink_GetDisplayName() {
        if (this.title != null && this.title != "")
            return this.title;
        else
            return this.listUrl;
    }
}


